﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UCOLOR.Helpers;
using UColor.WPF;
using UCOLOR.Model;
namespace UCOLOR.View
{
    /// <summary>
    /// HomeView.xaml 的交互逻辑
    /// </summary>
    public partial class HomeView :  UCModule 
    {
        string Category;
        private int Level = 1;
        public HomeView(int iLevel,string category,UColor.WPF.Internal.BaseControlModulesPage baseModules)
        {
            InitializeComponent();
            Level = iLevel;
            this.ParentModulePage = baseModules;
            Category = category;
            if (Level == 1)
                ShowDirCode();
            else
                ShowDirFamily();
         
        }
        public System.Collections.Hashtable tabList = new System.Collections.Hashtable();
       // private TabControl mTab;
        public void ShowDirCode()
        {
            IList<PartProductCode> list= DataHelper.GetPartProductCode();
         //   mTab = new TabControl();
            
            string key = "PartProduct";
            if (!tabList.ContainsKey(key))
            {
               
                mTab.Uid = "tab" + key;
                tabList.Add(key, mTab);
             //   this.dockPanelTab.Children.Add(mTab);                
               // IList<PartProductFamily> list = DataHelper.GetPartProductFamily(key);
                if (list != null)
                {
                    TabItem homeTab = new TabItem();
                    homeTab.Background = mTab.Background;
                    homeTab.Header = "首页";
                    mTab.Items.Add(homeTab);
                    homeTab.Tag = "ALL";
                    //tab.SelectedIndex = tab.Items.Count - 1;
                    ProductListView productView = new ProductListView();
                    productView.PhoteSources = list;
                    productView.ParentModulePage = this.ParentModulePage;
                    homeTab.Content = productView;
                    homeTab.Margin = new Thickness(2, 2, 2, 2);

                    foreach (PartProductCode part in list)
                    {
                        TabItem temptb = new TabItem();
                        temptb.Background = mTab.Background;
                        temptb.Header = part.description;
                        temptb.Tag = part.product_code;
                        mTab.Items.Add(temptb);
                        //tab.SelectedIndex = tab.Items.Count - 1;
                        productView = new ProductListView();
                        productView.ViewType = 2;
                        productView.ShowPartDataView(part.product_code);
                        productView.ParentModulePage = this.ParentModulePage;
                        temptb.Content = productView;
                        temptb.Padding = new Thickness(2, 2, 2, 2);
                       // productView.ResetTileStyle();
                       // temptb.Margin = new Thickness(2, 2, 2, 2);   
                    }
                }
            }
            else
            {
                mTab = tabList[key] as TabControl;
            }
            
        }
        public string ProductCode
        {
            get
            {
                if (mTab == null)
                    return"";
                TabItem tab = mTab.SelectedItem as TabItem;
                if (tab == null || tab.Tag == null)
                    return "";
                return tab.Tag.ToString();
            }
        }
       
        public void ShowDirFamily()
        {
            IList<PartProductCode> list = DataHelper.GetPartProductCode();
            

            string key = "PartProductCode";
            if (!tabList.ContainsKey(key))
            {
              //  mTab = new TabControl();
                mTab.Uid = "tab" + key;
                tabList.Add(key, mTab);
              //  this.dockPanelTab.Children.Add(mTab);
                
                if (list != null)
                {
                    //TabItem homeTab = new TabItem();
                    //homeTab.Header = "首页";
                    //tab.Items.Add(homeTab);
                    ////tab.SelectedIndex = tab.Items.Count - 1;
                    //ProductListView productView = new ProductListView();
                    //productView.PhoteSources = list;
                    //productView.ParentModulePage = this.ParentModulePage;
                    //homeTab.Content = productView;
                    //homeTab.Padding = new Thickness(2, 2, 2, 2);
                    int i = 0;
                    foreach (PartProductCode part in list)
                    {
                        i = i++;
                        TabItem temptb = new TabItem();
                        temptb.Background = mTab.Background;
                        temptb.Header = part.description;
                        temptb.Tag = part.product_code;
                        mTab.Items.Add(temptb);
                        TabControl famTab = new TabControl();
                        famTab.Background = mTab.Background;
                        if (part.product_code == Category)
                            mTab.SelectedItem = temptb;
                        temptb.Content = famTab;
                         IList<PartProductFamily> familys = DataHelper.GetPartProductFamily(part.product_code);
                         if (familys == null)
                             continue;
                         TabItem allTab = new TabItem();
                         allTab.Header = "全部";
                         allTab.Tag = "ALL";
                         famTab.Items.Add(allTab);
                         allTab.Background = mTab.Background;
                         //tab.SelectedIndex = tab.Items.Count - 1;
                         ProductListView productView = new ProductListView();
                         productView.ParentModulePage = this.ParentModulePage;
                         productView.PhoteSources = familys;
                         allTab.Content = productView;
                         allTab.Padding = new Thickness(2, 2, 2, 2);
                         if (part.product_code == Category)
                         {
                         }
                         foreach (PartProductFamily fay in familys)
                         {
                             //IList<PartProductFamily> list = DataHelper.GetPartProductFamily(key);
                             //if (list != null)
                             //{
                             //    foreach (PartProductFamily part in list)
                             //    {
                             //        TabItem temptb = new TabItem();
                             //        temptb.Header = part.product_family;
                             //        tab.Items.Add(temptb);
                             //        //tab.SelectedIndex = tab.Items.Count - 1;
                             //        ProductListView productView = new ProductListView(part.product_family);
                             //        productView.ParentModulePage = this.ParentModulePage;
                             //        temptb.Content = productView;
                             //        temptb.Padding = new Thickness(2, 2, 2, 2);
                             //    }
                             //}
                             
                             TabItem fTab = new TabItem();
                             fTab.Background = mTab.Background;
                             fTab.Header = fay.description;
                             famTab.Items.Add(fTab);
                             //tab.SelectedIndex = tab.Items.Count - 1;
                             productView = new ProductListView(fay.product_family);
                             productView.ViewType = 2;
                             //productView.ShowPartDataView(fay.product_family);
                             productView.ParentModulePage = this.ParentModulePage;
                             productView.ResetTileStyle();
                             fTab.Content = productView;
                             fTab.Padding = new Thickness(2, 2, 2, 2);

                         }
                    }
                }
            }
            else
            {
                mTab = tabList[key] as TabControl;
            }

        }

        private void Tile_Click(object sender, EventArgs e)
        {
           // ShowDir();
        }

        private void TileLayoutControl_TileClick(object sender, DevExpress.Xpf.LayoutControl.TileClickEventArgs e)
        {

        }

        private void Tile_Click_1(object sender, EventArgs e)
        {
           // ShowDir();
        }

        void ShowMoudel(MainWindow main, string Key)
        {
           // main.Content = main.ViewList[Key];
        }

        //void ShowMoudel(string Key)
        //{
        //    MainWindow main = (this.DataContext as MainWindow);
        //    ShowMoudel(main, Key);
        //}
        //[Ambient]
        //public ResourceDictionary Resources { get; set; }
    }
}
